package com.huluobo.a2107mvpimdemo;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.huluobo.a2107mvpimdemo.chat.ChatActivity;
import com.hyphenate.EMCallBack;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.interfaces.OnItemLongClickListener;
import com.hyphenate.easeui.modules.contact.EaseContactListFragment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ContactFragment extends EaseContactListFragment {
    private List<String> usernames = new ArrayList<>();

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EMClient.getInstance().contactManager().aysncGetAllContactsFromServer(new EMValueCallBack<List<String>>() {
            @Override
            public void onSuccess(List<String> value) {
                Collections.sort(value);//排个序
                usernames.addAll(value);
            }

            @Override
            public void onError(int error, String errorMsg) {
                Log.i("TagA", "errorMsg" + errorMsg);
            }
        });

        contactLayout.getContactList().getListAdapter().setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(View view, int position) {
                EMClient.getInstance().contactManager().aysncDeleteContact(usernames.get(position), new EMCallBack() {
                    @Override
                    public void onSuccess() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                contactLayout.getContactList().notifyDataSetChanged();
                                Toast.makeText(getActivity(), "删除好友成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onError(int code, String error) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getActivity(), "删除好友失败:" + error, Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
                return true;
            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        if (usernames.size() != 0) {
            String conversationId = usernames.get(position);
            Intent intent = new Intent(getActivity(), ChatActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("conversationId", conversationId);
            bundle.putInt("chatType", 1);
            intent.putExtras(bundle);
            getActivity().startActivity(intent);
        }
    }

}